var app=angular.module('myApp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/', {templateUrl: 'net_rate_mis.html', controller: ListtranCtrl}).
     otherwise({redirectTo: '/'});
}]);

function ListtranCtrl($scope, $http,$window) {    
    $scope.currentPage = 0;
    $scope.pageSize = 75;
	$scope.Math = window.Math;
	 //  $window.alert(pageSize);
    $http.get('net_rate_mis_data.php?q=1').success(function(data) {
		$scope.traninvalidmast = data;
		//$scope.traninvalidmast2 = data;
        //$window.alert(pageSize);
	});
    $scope.numberOfPages=function()
    {
        return Math.ceil($scope.data.length / $scope.pageSize);                
    };
    	
	/*$scope.csv3=function()
	{							
		var url=(window.location.pathname);
		var filename = url.substring((url.lastIndexOf('/')+1),(url.lastIndexOf('.')));			
		var tocsv = "exportcsv4.php";
		var z=document.getElementById("tbl");
		//alert(z.rows.length);
		$scope.content = z.innerHTML;
		$http.post(tocsv, { "data" : $scope.content}).
		success(function(data, status) {			
		//$scope.result = data;		
//alert(data);
			var csvBlob = new Blob([data], { type: 'text/csv;charset=utf-8' });
			saveAs(csvBlob, filename+'_export.csv');				
		});
	};*/
	
	 $scope.total_pl = function() {
        var total = 0;
        angular.forEach($scope.traninvalidmast, function(tran) {
         	total +=  tran.Dr_Amount - tran.netrate - tran.netrate2 -tran.netrate3;
        })
        return total;
	} 
	
	 $scope.total_debit = function() {
        var total = 0;
        angular.forEach($scope.traninvalidmast, function(tran) {
         	total += +tran.Dr_Amount;
        })
        return total;
	} 
	
	 $scope.total_nr = function() {
        var total = 0;
        angular.forEach($scope.traninvalidmast, function(tran) {
         	total += parseInt(tran.netrate) + parseInt(tran.netrate2) + parseInt(tran.netrate3);
        })
        return total;
	} 
	
	$scope.sumeach = function(a,b,c) {        
		return parseInt(a)+parseInt(b)+parseInt(c);        
	} 
	
	$scope.dfilter = function(from,to,markers) 
	{			
		//alert(from+":"+to+":"+markers.length);			
		$http.get('net_rate_mis_data.php?q=2').success(function(data) {
			$scope.dataset = data;		
		});
		markers=$scope.dataset;
		var retMarkers = [];		
		var j=parseInt(0);
		
		if(!from)
			from='0000-00-00';
		
		if(!to)
		{
			var d = new Date();					
			var d2 = d.getFullYear()+"-"+(parseInt(d.getMonth())+1)+"-"+d.getDate();			
			to=d2;
		}
		//alert(from+":"+to+":"+markers.length);
		//alert(from<markers[0].Date);
        for(var i = 0, len = markers.length; i < len; i++) 
		{
			var singleMarker = markers[i];
			//alert(singleMarker.Date >= from && singleMarker.Date <= to);
			if(singleMarker.Date >= from && singleMarker.Date <= to) 
			{			
				//retMarkers.push(singleMarker);
				retMarkers[j]=singleMarker;
				//alert(retMarkers[j].Referenceno);   
				j++;				    
			}
		}
		//alert($scope.traninvalidmast.length);
		$scope.traninvalidmast = retMarkers;
		$scope.traninvalidmast.length = j;
		
	}
/* 
	$scope.cancel = function() 
	{
		//location.reload(true);
		$scope.traninvalidmast=$scope.traninvalidmast2;
	}
    */
}
	
	/* 
	
	$scope.count = 0;
	$scope.$watch('traninvalidmast', function() {
	var cartTotal = 0;
	$scope.traninvalidmast.forEach(function(tran) {
      cartTotal += tran.total();
    });
   
   $scope.count += cartTotal;
	  //  if (traninvalidmast.length > 0) 
	
		//$window.alert(traninvalidmast.length);
	//for(count=0;count<$scope.traninvalidmast.length;count++)
		/* {
	//		$scope.count += count;	
			 total += $scope.traninvalidmast[count].netrate-$scope.traninvalidmast[count].Dr_Amount;
			 $scope.count += 1
			} */ 
	     // return total; 
		 
   // }, true);
 
	/* 
	$scope.$watch('traninvalidmast', function() {
    var cartTotal = 0;
    
    $scope.traninvalidmast.forEach(function(tran) {
      cartTotal += tran.total();
    });
    
    $scope.cartTotal = cartTotal;
  }, true); */
	
	/* 
	  $scope.totalP = function(){
			var total = 0;
			// $window.alert(traninvalidmast.length);
									 
			for(count=0;count<$scope.traninvalidmast.length;count++)
			{
				total += $scope.traninvalidmast[count].netrate-$scope.traninvalidmast[count].Dr_Amount;
			}
			return total;
		} */

		/* $scope.total = function() {
        var total = 0;
        angular.forEach($scope.traninvalidmast, function(tran) {
         	total += tran.netrate - tran.Dr_Amount;
        })

        return total;
	    }  */
     
	  
	  /* $scope.totalLength = function() {
        return $scope.traninvalidmast.reduce(function(current, tran) { return current + tran.length; }, 0);
    }; */

	  
	  
	 

    app.directive('capitalize', function () {
        return {
            require: 'ngModel',
            link: function (scope, element, attrs, modelCtrl) {
                var capitalize = function (inputValue) {
                    if (angular.isUndefined(inputValue))
                        return;
 
                    var capitalized = inputValue.toUpperCase();
                    if (capitalized !== inputValue) {
                        modelCtrl.$setViewValue(capitalized);
                        modelCtrl.$render();
                    }
                    return capitalized;
                };
                modelCtrl.$parsers.push(capitalize);
                capitalize(scope[attrs.ngModel]);  // capitalize initial value
            }
        };
    });
    
	
//We already have a limitTo filter built-in to angular,
//let's make a startFrom filter
app.filter('startFrom', function() {
    return function(input, start) {
        start = +start; //parse to int
        return input.slice(start);
    };
});

/* 
app.factory('Item', function() {
  function Item() {
    this.total = function() {
      return (this.qty * this.prize) || 0;
    }
  }
  
  return Item;
});

 */

